#include "avatar_widget.h"
#include "ui_avatar_widget.h"

#include <QPainter>

AvatarWidget::AvatarWidget(const QString &name, const bool &is_add, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AvatarWidget), name_(name), is_add_(is_add), is_read_only_(false), is_checked_(false)
{
    ui->setupUi(this);

    if(is_add)
    {
        name_ = QString("添加新玩家");
        uuid_ = "INSERT";
    }
}

AvatarWidget::AvatarWidget(const QString &uuid, const QString &name, QWidget *parent)
    : QWidget(parent), uuid_(uuid), name_(name), is_add_(false), is_read_only_(false), is_checked_(false)
{

}

AvatarWidget::AvatarWidget(QWidget *parent)
    : QWidget(parent), is_add_(false), is_read_only_(true)
{

}

AvatarWidget::~AvatarWidget()
{
    delete ui;
}

void AvatarWidget::SetName(const QString &name)
{
    name_ = name;
    update();
}

QSize AvatarWidget::sizeHint() const
{
    return QSize(120, 120);
}

void AvatarWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setPen(Qt::NoPen);

    int diameter = qMin(this->height(), this->width()) / 2;
    int center_x = (this->width() - diameter) / 2;
    int center_y = (this->height() - diameter) / 2;

    QBrush brush(Qt::gray);
    painter.setBrush(brush);

    //draw avatar img
    painter.drawEllipse(center_x, center_y, diameter, diameter);
    painter.save();

    //fill name in circle
    QString name_abbr;
    if(!is_add_)
    {
        if(name_.isEmpty())
        {
            name_abbr = "";
        }
        else
        {
            name_abbr = name_[0];
        }
    }
    else
    {
        name_abbr = QString("+");
    }

    QFont font(this->font());
    font.setPointSize(diameter / 2);
    painter.setFont(font);

    QFontMetrics metrics = painter.fontMetrics();
    int textWidth = metrics.horizontalAdvance(name_abbr);
    int textHeight = metrics.height();

    painter.setPen(Qt::white);
    painter.setBrush(Qt::white);

    // 计算文本的左上角坐标，使其居中
    int textX = (center_x + diameter / 2) - textWidth / 2;
    int textY = (center_y + diameter / 2) - (textHeight / 2) + metrics.ascent(); // ascent() 使文本基线对齐
    painter.drawText(textX, textY, name_abbr);

    //render text under
    painter.restore();
    font = this->font();
    font.setPointSize(diameter / 15);

    painter.setFont(font);
    metrics = painter.fontMetrics();

    QString real_name;
    if(is_checked_ && !is_add_)
    {
        real_name = QString("%1 (已选)").arg(name_);
        painter.setPen(QColor("#32B67A"));
    }
    else
    {
        real_name = name_;
        painter.setPen(QColor("#1B1D1C"));
    }

    textWidth = metrics.horizontalAdvance(real_name);
    textHeight = metrics.height();
    textX = (center_x + diameter / 2) - textWidth / 2;

    painter.drawText(textX, center_y + diameter + textHeight, real_name);

    QWidget::paintEvent(event);
}

void AvatarWidget::mouseReleaseEvent(QMouseEvent *event)
{
    if(!is_read_only_)
    {
        is_checked_ = !is_checked_;
        emit signal_btn_checked(uuid_, is_checked_);

        update();
    }

    QWidget::mouseReleaseEvent(event);
}
